﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Component;

public partial class BackEnd_ColorL : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        DataBin(ColorDB.GetAll(true));
    }
    void DataBin(object oj)
    {
        grid.DataKeyNames = new string[] { ColumColors._Id };
        grid.DataSource = oj;
        grid.DataBind();

    }
    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBin(ColorDB.GetAll(true));
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        DataBin(ColorDB.GetAll(true));
    }
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;
        DropDownList ddlAct = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlAct");
        TextBox txtName = (TextBox)grid.Rows[e.RowIndex].FindControl("txtName");
        TextBox txtValue = (TextBox)grid.Rows[e.RowIndex].FindControl("txtValue");
        Colors color = ColorDB.GetObjectById(ConvertType.ToInt(id), true);
        color.Active = ConvertType.ToBool(ddlAct.Text);
        color.Name = txtName.Text.Trim();
        color.Value = txtValue.Text;
        if (ColorDB.Save(color) == 1)
        {
            this.labMessage.Text = ConstString._ThucHienTC;
        }
        Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (string key in Request.Form.AllKeys)
        {
            if (key.Contains("check") == true)
            {
                Colors color = ColorDB.GetObjectById(ConvertType.ToInt(key.Replace("check", "")), true);
                if (color != null)
                {
                    ColorDB.Delete(color);
                }

            }
            DataBin(ColorDB.GetAll(true));
        }

    }
}
